package com.shuyun.demo1;

import io.reactivex.rxjava3.core.Maybe;
import io.reactivex.rxjava3.core.Observable;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

/**
 * @description:
 * @author: shuai.chen@shuyun.com
 * @date: 2023/1/13 3:06 PM
 */
public class CreateDemo {

    /**
     * Creating an Observable via the create() method
     */
    @Test
    public void create() {
        Observable<String> observable = Observable.create(emitter -> {
            try {
                List<String> list = Arrays.asList("jerry", "tom");
                for (String s : list) {
                    emitter.onNext(s);
                }
                emitter.onComplete();
            } catch (Exception e) {
                emitter.onError(e);
            }
        });
        observable.subscribe(s -> System.out.println(s));
    }

    @Test
    public void createByMaybe() {
        Maybe<List> listMaybe = Maybe.create(emitter -> {
            try {
                List<String> list = Arrays.asList("jerry", "tom");
                if (list.isEmpty()) {
                    emitter.onComplete();
                } else {
                    emitter.onSuccess(list);
                }
            } catch (Exception e) {
                emitter.onError(e);
            }
        });
        listMaybe.blockingGet().forEach(s -> {
            System.out.println(s);
        });
    }

}
